using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;
using BitmapFont = FlatRedBall.Graphics.BitmapFont;
using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
using Color = Microsoft.Xna.Framework.Color;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;


#endif

namespace MP3.Entities
{
	public partial class PlayerBall
	{
        public Xbox360GamePad mGamePad; // assign game pad to each player ball
        private const int MAX_DRAG = 10;    // max friciton
        private const int MIN_DRAG = MAX_DRAG / 8;  // min frcition

        // player index for teams / controlelr number
        public int PlayerIndex 
        {
            set
            {
                mGamePad = InputManager.Xbox360GamePads[value];
            }
        }

		private void CustomInitialize()
		{
            PlayerIndex = 0;
		}

		private void CustomActivity()
		{
            CheckStates();
            UpdateMovingBall();
            QuitGame();
            MovementActivity();
            UpdateColor();
            UpdateMass();
		}

		private void CustomDestroy()
		{


		}

        private static void CustomLoadStaticContent(string contentManagerName)
        {


        }

        // allows movement with leftThumbstick -- accleration
        private void MovementActivity()
        {
            mGamePad.ControlPositionedObjectAcceleration(this, movementSpeed);
        }


        // button for quiting the game
        private void QuitGame()
        {
            if (mGamePad.ButtonPushed(Xbox360GamePad.Button.Back))
                FlatRedBallServices.Game.Exit();       
        }

        // update color of the ball using rightThumbstick
        private void UpdateColor()
        {
           
            Color mColor = BodyColor;

            // increase color
            if (mGamePad.RightStick.Position.X > 0)
            {
                if ((byte)mGamePad.RightStick.Magnitude + mColor.R > 255)
                    mColor.R = (byte)255;
                else 
                    mColor.R += (byte)mGamePad.RightStick.Magnitude;
                BodyColor = mColor;
                Tex.Alpha = (float)(BodyColor.R /255f);
            }

             // decrease color
            else if (mGamePad.RightStick.Position.X < 0)
            {
                if (mColor.R - (byte)mGamePad.RightStick.Magnitude < 0)
                    mColor.R = 0;
                else 
                    mColor.R -= (byte)(mGamePad.RightStick.Magnitude);
                BodyColor = mColor;
                Tex.Alpha = (float)(BodyColor.R /255f);
            }
        }


        // update mass of the playerball according to the red component of the playerball
        private void UpdateMass()
        {

            Mass = (float)BodyColor.R / 25f;
        }

        // check for FSM
        private void CheckStates()
        {
            if (mGamePad.ButtonPushed(Xbox360GamePad.Button.A))
            {
                Velocity = Vector3.Zero;
                Drag = MIN_DRAG;
                mCurrentState = VariableState.Moving;
            }
            else if (mGamePad.ButtonPushed(Xbox360GamePad.Button.B))
            {

                if (mCurrentState == VariableState.Moving)
                {
                    Drag = MAX_DRAG;
                    mCurrentState = VariableState.Stationary;
                }
                else
                {
                    Drag = MIN_DRAG;
                    mCurrentState = VariableState.Moving;
                }
            }
        }

        // update ball when in Moving State
        private void UpdateMovingBall()
        {
            if (mCurrentState == VariableState.Moving)
            {
                if (mGamePad.LeftStick.Position.Y > 0)
                {
                    movementSpeed = mGamePad.LeftStick.Position.Y * 50;
                
                }
                YVelocity += Gravity;
                RotationZ = Position.X * Gravity * 2 / 3f;
            }
            else
            {
                movementSpeed = 350;
                Gravity = 0;
            }
        }
	}
}
